<?php
 
include_once("./configure/configure.php");		//--> global var
include_once("Smarty.class.php");				//--> out template

include_once("utils/FileUtils.php");			//--> file utils

include_once("impl/CouponImpl.php");
include_once("impl/CouponAttributeImpl.php");

include_once("impl/CategoryImpl.php");
include_once("impl/MerchantImpl.php");
include_once("utils/AlgorithmUtils.php");

include_once("constant/GlobalConstant.php");


include_once("includeSmarty.php");


if(isset($_POST['id']))
	$id = $_POST['id'];
else
	$id = $_GET['id'];

if(isset($_POST['page']))
	$page 	= $_POST['page'];
else
	$page	= 0;

if(empty($id)){
	$location = HOME_URL_HTTP;
	header("Location: $location");
				
	exit;
}

$id	= str_replace('-', '_', $id);

$cacheId 	= $id . '_' . $page;

/*----- out html -----*/
$SUB_DIR	= FileUtils::getDirByString($id) . '/';

$smarty->compile_dir  	= COMPILE_DIR . '/mer/' . $SUB_DIR;
$smarty->cache_dir  	= CACHE_DIR . '/mer/' . $SUB_DIR;


if(!$smarty->isCached('merchant.html', $cacheId, $cacheId)){	
	$oCouponImpl 		= new CouponImpl(DB_TAG_COUPON);
	$oCAttributeImpl 	= new CouponAttributeImpl(DB_TAG_COUPON);
	
	$oCategoryImpl		= new CategoryImpl(DB_TAG_COUPON);
	$oMerchantImpl 		= new MerchantImpl(DB_TAG_COUPON);
	
	include_once("includeTag.php");
	include_once("includeCategories.php");

	
	$oMerchant 	= $oMerchantImpl->getByNameId($id);
	if(is_object($oMerchant)){
		$oLogo 	= $oMerchantImpl->getLogo($oMerchant->getId());
		if(!is_object($oLogo)){
			$oLogo = new ImageDomain();
			
			$oLogo->setId(0);
			$oLogo->setExtName('gif');
		}
		
		$oMerchant->setLogo($oLogo);
	}else{
		$location = HOME_URL_HTTP;
		header("Location: $location");
					
		exit;
	}
	
	$oRelatedMerchants = $oMerchantImpl->getPopularByRelatedCategory($oMerchant->getId(), 0, 30);
	if(count($oRelatedMerchants) == 0)
		$oRelatedMerchants = $oMerchantImpl->getPopular(0, 30);
		

	// get recommended coupon list of merchant
	$oRecommendedCoupons = $oCouponImpl->getRecommendedCoupons($oMerchant->getId(), $page, 15);
	
	foreach ($oRecommendedCoupons as $oCoupon) {
		$oCouponAttribute = $oCAttributeImpl->getLists($oCoupon->getId());
		$oCoupon->setAttributes($oCouponAttribute);
			
		$oCoupon->setMerchant($oMerchant);
		
		$oCategory 	= $oCategoryImpl->getByCouponId($oCoupon->getId());
		if(!is_object($oCategory))
			$oCategory = new CategoryDomain();
			
		$oCoupon->setCategory($oCategory);
		
		if($oCoupon->getCreativeType() == 1){
			$oImage = $oCouponImpl->getCouponImage($oCoupon->getId());
			
			if(is_object($oImage)){
				$oCoupon->setCouponImage($oImage);
			}
		}
	}

	// get featured coupon list of merchant
	$oFeaturedCoupons = $oCouponImpl->getFeaturedCoupons($oMerchant->getId(), $page, 15);
	
	foreach ($oFeaturedCoupons as $oCoupon) {
		$oCouponAttribute = $oCAttributeImpl->getLists($oCoupon->getId());
		$oCoupon->setAttributes($oCouponAttribute);
			
		$oCoupon->setMerchant($oMerchant);
		
		$oCategory 	= $oCategoryImpl->getByCouponId($oCoupon->getId());
		if(!is_object($oCategory))
			$oCategory = new CategoryDomain();
			
		$oCoupon->setCategory($oCategory);
		
		if($oCoupon->getCreativeType() == 1){
			$oImage = $oCouponImpl->getCouponImage($oCoupon->getId());
			
			if(is_object($oImage)){
				$oCoupon->setCouponImage($oImage);
			}
		}
	}
	
	
	// get coupon list for the merchant
	$oCoupons = $oCouponImpl->getCommonCoupons($oMerchant->getId(), $page, 15);	
	
	
	foreach ($oCoupons as $oCoupon) {
		$oCouponAttribute = $oCAttributeImpl->getLists($oCoupon->getId());
		$oCoupon->setAttributes($oCouponAttribute);
			
		$oCoupon->setMerchant($oMerchant);
		
		$oCategory 	= $oCategoryImpl->getByCouponId($oCoupon->getId());
		if(!is_object($oCategory))
			$oCategory = new CategoryDomain();
			
		$oCoupon->setCategory($oCategory);
		
		if($oCoupon->getCreativeType() == 1){
			$oImage = $oCouponImpl->getCouponImage($oCoupon->getId());
			
			if(is_object($oImage)){
				$oCoupon->setCouponImage($oImage);
			}else{
				$oCoupon->setCreativeType(2);
			}
		}
	}
	
	$smarty->assign('oCoupons', $oCoupons);
	$smarty->assign('oFeaturedCoupons', $oFeaturedCoupons);
	$smarty->assign('oRecommendedCoupons', $oRecommendedCoupons);
	
	$smarty->assign('oMerchant', $oMerchant);
	
	$smarty->assign('oRelatedMerchants', $oRelatedMerchants);
	$smarty->assign('oTags', $oTags);
	$smarty->assign('oCategories', $oCategories);
	
	$smarty->assign('pageList', $oCouponImpl->getPageList(5));
	$smarty->assign('nextPage', $oCouponImpl->getNextPage());
	$smarty->assign('prePage', $oCouponImpl->getPrePage());
	$smarty->assign('pageCount', $oCouponImpl->getPageCount());
	$smarty->assign('page', $oCouponImpl->getPage());
	
}

$smarty->assign('HOME_URL', HOME_URL);
$smarty->assign('HOME_URL_HTTP', HOME_URL_HTTP);

$smarty->assign('id', $id);

$smarty->display('merchant.html', $cacheId, $cacheId);
?>
